<?php
/**
 * 本文件用来收集常用函数的定义
 * 包括:
 * 114电话查询、ip地址查询、电话归属地查询、身份证归属地查询，使用时用类即可
 * 部分内容使用的是 k780 的数据，账号密码件另外文件
 * 
 */

require_once(dirname(__FILE__) . "/../inc/account_info.php");

class SearchTools
{
	var $input;	// 输入
	var $result;		// 结果输出
	
	function __construct($in)
	{
		$args_num = func_num_args(); // 获取参数个数
		if($args_num < 1)
		{
			exit("func param is less than 1, exit now!");			
		}
		$this->input = $in;
	}
	
	function getResult()
	{
		return $this->result;
	}
	
	/**
	 * 由于返回数据的json并不规范，可能没有双引号啥的，json_decode 都会报错
	 * 此函数用于广义解析json，补充格式
	 */	
	function no_check_json_decode($str, $mode=false)
	{
		$str = $this->GBK2UTF8($str);
		
		// 处理 {a:"b"}形式
		if(preg_match('/([0-9a-zA-Z\x{4e00}-\x{9fa5}]+):/u', $str))
		{
			$str = preg_replace("/([0-9a-zA-Z\x{4e00}-\x{9fa5}]+):/us", '"$1":', $str);
		}
		
		
		// 处理 {"a":b}形式
		if(preg_match('/:([0-9a-zA-Z\x{4e00}-\x{9fa5}]+)/u', $str))
		{
			$str = preg_replace('/:([0-9a-zA-Z\x{4e00}-\x{9fa5}]+)/us', ':"$1"', $str);  
		}
		
		// 处理 {'a':'b'} 或一个单引号的形式
		if(preg_match("/\'([0-9a-zA-Z\x{4e00}-\x{9fa5}]+)\'/u", $str))
		{
			$str = preg_replace('/\'([0-9a-zA-Z\x{4e00}-\x{9fa5}]+)\'/us', '"$1"', $str);  
		}
		
		return json_decode($str, $mode);  
	}
	
	/** 
	 *自动判断把gbk或gb2312编码的字符串转为utf8 
	 *能自动判断输入字符串的编码类，如果本身是utf-8就不用转换，否则就转换为utf-8的字符串 
	 *支持的字符编码类型是：utf-8,gbk,gb2312 
	 *@$str:string 字符串 
	 */ 
	function GBK2UTF8($str)
	{ 
		$charset = mb_detect_encoding($str, array('UTF-8','GBK','GB2312')); 
		$charset = strtolower($charset); 
		if('cp936' == $charset)
		{ 
			$charset='GBK'; 
		} 
		if("utf-8" != $charset)
		{ 
			$str = iconv($charset,"UTF-8//IGNORE",$str); 
		} 
		return $str; 
	}
	
	// 获取身份证数据
	function locateIDCard()
	{
		// 从k780获取数据，使用https接口
		$url = 'https://sapi.k780.com/?app=idcard.get&format=json&appkey=' 
				. K780_APPID . '&sign=' . K780_SIGN . '&idcard=' . $this->input;
		
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_ENCODING, 'utf-8');
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		
		$location = curl_exec($ch);	// json返回
		curl_close($ch);
		
		$location = $this->no_check_json_decode($location);
		
		if(!empty($location->success) && ($location->success==1))
		{
			$this->result = $location->result->style_simcall .  ", 详细: " . $location->result->sex 
				. ", " . $location->result->born . ", " . $location->result->att;
			
		}
		else
		{
			$this->result = "服务器返回错误，请重试.";
		}
			
	}
	
	
	// 获取ip地址归属地
	function locateIpaddr()
	{
		// 从新浪获取数据，腾讯也行，网上一搜一大把的
		$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip='.$this->input.'&format=json';
		
		// 调用php的socket获取返回数据
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_ENCODING, 'utf-8');
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		
		$location = curl_exec($ch);	// json返回
		curl_close($ch);
		
		$location = $this->no_check_json_decode($location);
		if($location == false)
		{
			return;			
		}
		
		if(!empty($location->desc))
		{
			$this->result = $location->desc;			
		}
		else if(!empty($location->country))
		{
			$this->result = $location->country . $location->province . $location->city . $location->district . $location->isp;
		}
		else
		{
			$this->result="服务器返回错误，请重试.";
		}
	}
	
	// 获取手机号码归属地查询
	function locatePhoneaddr()
	{
		$url = 'http://virtual.paipai.com/extinfo/GetMobileProductInfo?amount=10000&mobile='.$this->input;
		
		// 调用php的socket获取返回数据
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_ENCODING, 'utf-8');
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		
		$location = curl_exec($ch);	// json返回
		curl_close($ch);
		
		preg_match_all("/\{mobile.*\'\}/", $location, $matches);
		
		$location = $this->no_check_json_decode($matches[0][0]);

		if($location == false)
		{
			$this->result="服务器返回错误，请重试.";
			return;			
		}
		
		if(!empty($location->isp))
		{
			$this->result = $location->isp . ", " . $location->province . $location->cityname;
		}
		else
		{
			$this->result="查询错误，请重试.";
		}
	}
}

$st = new SearchTools("");
$st->locateIDCard();

?>
